package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.common.exception.BlogException;
import com.example.blog.common.pojo.dataobject.BlogInfo;
import com.example.blog.common.pojo.dataobject.UserInfo;
import com.example.blog.common.pojo.request.UserLoginParam;
import com.example.blog.common.pojo.response.UserInfoResponse;
import com.example.blog.common.pojo.response.UserLoginResponse;
import com.example.blog.common.utils.BeanConver;
import com.example.blog.common.utils.JwtUtil;
import com.example.blog.common.utils.SecurityUtil;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.service.BlogService;
import com.example.blog.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

//被@Service注解标记，所以Spring会创建UserServiceImpl的实例作为Bean。
@Service
public class UserServiceImpl implements UserService {
    @Resource(name="userInfoMapper")
    private UserInfoMapper userInfoMapper;

    @Resource(name="blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Resource(name="blogServiceImpl")
    private BlogServiceImpl blogServiceImpl;
    @Override
    public UserLoginResponse login(UserLoginParam userParam) {
        //判断用户是否存在
        //根据用户名,去查询用户信息
        //selectOne方法是MyBatis Plus提供的一个方法，用于查询单条记录
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                //映射到数据库的username字段,从参数对象获取用户名。         映射到delete_flag字段
                .eq(UserInfo::getUserName, userParam.getUserName()).eq(UserInfo::getDeleteFlag, 0));
        //如果用户不存在
        if(userInfo==null){
           throw new BlogException("用户不存在");
        }

        //用户存在,检验密码是否正确
        if(!SecurityUtil.verify(userParam.getPassword(),userInfo.getPassword())){
            throw new BlogException("密码错误");
        }


        //密码正确
        UserLoginResponse response=new UserLoginResponse();
        //将查询到的用户ID（userInfo.getId()）设置到响应对象中，通常用于前端后续操作的身份标识。
        response.setUserId(userInfo.getId());
        //载荷
        Map<String,Object> map=new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",userInfo.getUserName());
        response.setToken(JwtUtil.genToken(map));
        return response;
    }

    @Override
    public UserInfoResponse getUserInfoById(Integer userId) {
        UserInfo userInfo=selectUserInfoById(userId);
        return BeanConver.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfoById(Integer blogId) {
        //1.根据博客id,拿到作者id
        BlogInfo blogInfo=blogServiceImpl.selectBlogById(blogId);
        if(blogInfo==null){
            throw new BlogException("博客不存在");
        }
        //2.根据作者id,拿到作者详情
        UserInfo userInfo=selectUserInfoById(blogInfo.getUserId());
        return BeanConver.trans(userInfo);
    }


    public UserInfo selectUserInfoById(Integer userId){
         return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId,userId).eq(UserInfo::getDeleteFlag,0));
    }
}














